package com.cy.ppj.service.manager.impl;

import com.cy.ppj.comm.constant.Constants;
import com.cy.ppj.comm.utils.CommonUtils;
import com.cy.ppj.dao.mapper.manager.ManagerMapper;
import com.cy.ppj.model.PageBean;
import com.cy.ppj.model.Result;
import com.cy.ppj.model.domian.admin.Manager;
import com.cy.ppj.model.dto.manager.ManagerLoginDTO;
import com.cy.ppj.model.enums.ManagerStatus;
import com.cy.ppj.model.enums.ManagerType;
import com.cy.ppj.model.query.AuditManagerQuery;
import com.cy.ppj.model.query.ManagerAdminQuery;
import com.cy.ppj.model.vo.AuditManagerVO;
import com.cy.ppj.model.vo.ManagerAdminVO;
import com.cy.ppj.service.BaseService;
import com.cy.ppj.service.manager.ManagerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @description: 管理员
 * @author: Ocean
 * @create: 2018-07-31 20:22
 **/
@Service
public class ManagerServiceImpl extends BaseService implements ManagerService {

    @Autowired
    private ManagerMapper managerMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    @Transactional
    public void addManger(Manager manager) {
    	//验证手机号码是否已存在
    	Manager dbManager = new Manager();
    	dbManager.setMobile(manager.getMobile());
    	dbManager = managerMapper.selectOne(dbManager);
    	state(dbManager == null, "该用户手机号码已存在");
    	
        manager.setStatus(ManagerStatus.ENABLE);
        manager.setCreateTime(new Date());
        manager.setPassword(CommonUtils.md5Passwod(Constants.USER_INIT_PASSWORD));
        managerMapper.insertSelective(manager);
    }

    @Override
    @Transactional
    public void updateManager(Manager manager) {
        manager.setUpdateTime(new Date());
        managerMapper.updateByPrimaryKeySelective(manager);
    }

    @Override
    public Result login(Manager manager) {

        manager.setPassword(CommonUtils.md5Passwod(manager.getPassword()));
        manager = managerMapper.selectOne(manager);
        state(manager != null, "手机号或密码错误");

        //判断管理员账户状态
        switch (manager.getStatus()) {
	        case DISABLE:
	            return Result.fail("该账户已被禁用，请联系超级管理员");
	
	        case DELETE:
	        	return Result.fail("不存在该账户");
	        	
	        default:
	            break;
	    }
        
        //生成token
        String token = UUID.randomUUID().toString();

        ManagerLoginDTO managerLoginDTO = new ManagerLoginDTO();
        managerLoginDTO.setManagerId(manager.getId());
        managerLoginDTO.setRealName(manager.getRealName());
        managerLoginDTO.setMobile(manager.getMobile());
        managerLoginDTO.setToken(token);
		managerLoginDTO.setRoleId(manager.getRoleId());
		managerLoginDTO.setType(manager.getType());
		managerLoginDTO.setCityId(manager.getCityId());
		managerLoginDTO.setCompanyId(manager.getCompanyId());

        //存入Redis
        String key = Constants.ADMIN_LOGIN_KEY.concat(token);
        redisTemplate.opsForValue().set(key, managerLoginDTO, Constants.LOGIN_TIME, Constants.LOGIN_TIME_UNIT);

        return Result.success("登录成功", managerLoginDTO);
    }

	@Override
	public PageBean<AuditManagerVO> selectAuditManagerList(AuditManagerQuery auditManagerQuery) {
		int total = managerMapper.selectAuditManagerListCount(auditManagerQuery);
		List<AuditManagerVO> list = new ArrayList<>();
		if(total > 0) {
			auditManagerQuery.startPage();
			list = managerMapper.selectAuditManagerList(auditManagerQuery);
		}
		return new PageBean<>(list, total);
	}

	@Override
	public PageBean<ManagerAdminVO> selectManagerAdminList(ManagerAdminQuery query) {
		int total = managerMapper.selectManagerAdminCount(query);
		List<ManagerAdminVO> list = new ArrayList<>();
		if(total > 0) {
			query.startPage();
			list = managerMapper.selectManagerAdminList(query);
			list.stream().forEach(m -> {
				m.setTypeRemark(m.getType().getRemark());
			});
		}
		return new PageBean<>(list, total);
	}

	@Override
	@Transactional
	public void updateStatus(Long managerId, ManagerStatus status) {
		Manager manager = new Manager();
		manager.setId(managerId);
		manager.setStatus(status);
		manager.setUpdateTime(new Date());
		managerMapper.updateByPrimaryKeySelective(manager);
	}
	
	@Override
	@Transactional
	public void resetManagerPwd(Long managerId) {
		Manager manager = new Manager();
		manager.setId(managerId);
		manager.setPassword(CommonUtils.md5Passwod(Constants.USER_INIT_PASSWORD));
		manager.setUpdateTime(new Date());
		managerMapper.updateByPrimaryKeySelective(manager);
	}

	@Override
	@Transactional
	public void updatePassword(Long managerId, String oldPassword, String newPassword) {
		String md5OldPassword = CommonUtils.md5Passwod(oldPassword);
        String md5NewPassword = CommonUtils.md5Passwod(newPassword);
		// 验证旧密码
        Manager dbManager = new Manager();
		dbManager.setId(managerId);
		dbManager.setPassword(md5OldPassword);
		dbManager = managerMapper.selectOne(dbManager);
        state(dbManager != null, "旧密码不正确");
        
        // 修改新密码
        Manager manager = new Manager();
        manager.setId(managerId);
        manager.setPassword(md5NewPassword);
        manager.setUpdateTime(new Date());
        managerMapper.updateByPrimaryKeySelective(manager);
        
	}
}
